package org.pussinboots.morning.cms.controller.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.pussinboots.morning.cms.common.result.CmsPageResult;
import org.pussinboots.morning.cms.common.result.CmsResult;
import org.pussinboots.morning.cms.common.security.AuthorizingUser;
import org.pussinboots.morning.cms.common.util.SingletonLoginUtils;
import org.pussinboots.morning.common.base.BaseController;
import org.pussinboots.morning.common.constant.CommonReturnCode;
import org.pussinboots.morning.common.support.page.PageInfo;
import org.pussinboots.morning.user.pojo.vo.UserPageDTO;
import org.pussinboots.morning.user.service.IUserShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
*
* 项目名称：morning-cms-web Maven Webapp
* 类名称：MemberListController
* 类描述：会员信息表示层控制器
* 创建人：bigbug
* 创建时间：2017年9月19日 
*
*/
@Controller
@RequestMapping(value = "/system/user")
@Api(value = "会员信息列表", description = "会员信息列表")
public class SystemUserListController extends BaseController{

	@Autowired
	private IUserShopService userShopService;
	
	/*@Autowired
	private IUserService userService;*/
	/**
	 * GET 会员列表页面
	 * @param model
	 * @return
	 */
	@ApiOperation(value = "会员列表页面", notes = "会员列表页面")
	@RequiresPermissions("user:list:view")
	@GetMapping(value = "/list/view")
	public String getListPage(Model model) {
		return "/modules/systemUser/system_user_list";
	}
	
	/**
	 * GET 管理员列表
	 * @return
	 */
	@ApiOperation(value = "获取会员列表", notes = "根据分页信息/搜索内容获取会员列表")  
	@RequiresPermissions("user:list:view")
	@GetMapping(value = "/list")
	@ResponseBody
	public Object listUser(PageInfo pageInfo, @RequestParam(required = false, value = "search") String search) {
		UserPageDTO userPageDTO = userShopService.listByPage(pageInfo, search);
		return new CmsPageResult(userPageDTO.getUserVOs(), userPageDTO.getPageInfo().getTotal());
	}
	
	
	/**
	 * PUT 启用/冻结会员
	 * @return
	 */
	@ApiOperation(value = "启用/冻结会员", notes = "根据url会员ID启动/冻结会员")
	/*@RequiresPermissions("administrator:list:audit")
	*/@PutMapping(value = "/{userId}/audit")
	@ResponseBody
	public Object audit(@PathVariable("userId") Long userId) {
		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		if (authorizingUser != null) {
			Integer count = userShopService.updateStatus(userId);
			return new CmsResult(CommonReturnCode.SUCCESS, count);
		} else {
			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
		}
	}
	
}
